package org.dromara.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.app.appReturn.AppMoBanReturn;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkYueJuDemoBo;
import org.dromara.app.domain.vo.SkYueJuDemoVo;
import org.dromara.app.domain.SkYueJuDemo;
import org.dromara.app.mapper.SkYueJuDemoMapper;
import org.dromara.app.service.ISkYueJuDemoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 约局模版Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-27
 */
@RequiredArgsConstructor
@Service
public class SkYueJuDemoServiceImpl implements ISkYueJuDemoService {

    private final SkYueJuDemoMapper baseMapper;

    /**
     * 查询约局模版
     *
     * @param id 主键
     * @return 约局模版
     */
    @Override
    public SkYueJuDemoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询约局模版列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 约局模版分页列表
     */
    @Override
    public TableDataInfo<SkYueJuDemoVo> queryPageList(SkYueJuDemoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkYueJuDemo> lqw = buildQueryWrapper(bo);
        Page<SkYueJuDemoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的约局模版列表
     *
     * @param bo 查询条件
     * @return 约局模版列表
     */
    @Override
    public List<SkYueJuDemoVo> queryList(SkYueJuDemoBo bo) {
        LambdaQueryWrapper<SkYueJuDemo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkYueJuDemo> buildQueryWrapper(SkYueJuDemoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkYueJuDemo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), SkYueJuDemo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SkYueJuDemo::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SkYueJuDemo::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTwoType()), SkYueJuDemo::getTwoType, bo.getTwoType());
        return lqw;
    }

    /**
     * 新增约局模版
     *
     * @param bo 约局模版
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkYueJuDemoBo bo) {
        SkYueJuDemo add = MapstructUtils.convert(bo, SkYueJuDemo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改约局模版
     *
     * @param bo 约局模版
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkYueJuDemoBo bo) {
        SkYueJuDemo update = MapstructUtils.convert(bo, SkYueJuDemo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkYueJuDemo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除约局模版信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<AppMoBanReturn> getYueJuDemoList(HttpServletRequest req) {
        List<AppMoBanReturn> listAll = new ArrayList<>();
        List<SkYueJuDemoVo> list = baseMapper.selectVoList();
        listAll = BeanUtil.copyToList(list,AppMoBanReturn.class);
        return listAll;
    }
}
